import { Component, ViewChild, Injector, Output, EventEmitter, ElementRef } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import {
    ReportServiceProxy,
    ModelEditDto,
    ChReportEditDto,
    GetChReportEditInput,
    GetSzReportEditInput
} from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';

@Component({
    selector: 'createOrEditSzReportModal',
    templateUrl: './create-or-edit-szreport.modal.component.html',
    styleUrls: ['./create-or-edit-szreport.modal.component.less']
})
export class CreateOrEditSzReportModalComponent extends AppComponentBase {
    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;

    tabledata;
    selectedDate = new Date();
    constructor(injector: Injector, private _reportServiceProxy: ReportServiceProxy) {
        super(injector);
    }

    show(modelCode): void {
        this.active = true;

        this._reportServiceProxy.getSzReportForEdit().subscribe(result => {
            this.tabledata = result;
        });
        this.modal.show();
    }

    save(): void {
        this.saving = true;
        let input = new GetSzReportEditInput();
        input.szReportEditDtoList = this.tabledata;
        input.year = this.selectedDate.getFullYear();
        input.month = this.selectedDate.getMonth() + 1;
        this._reportServiceProxy
            .creatOrUpdateSzReport(input)
            .pipe(finalize(() => (this.saving = false)))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }
}
